
using System;
using System.Collections.Generic;
using Godot;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public partial class ConfigManager
{

    private static ConfigManager _instance = null;

    private GodotObject dbOject = null;

    private Dictionary<string, List<JObject>> configDict = new Dictionary<string, List<JObject>>();
    public static ConfigManager getInstance()
    {
        if (_instance == null)
        {
            _instance = new ConfigManager();
        }
        return _instance;
    }

    ///<remarks>
    ///初始化所有配置
    ///</remarks>
    public void init()
    {
        GDScript gds = GD.Load<GDScript>("res://data/database.gd");
        this.dbOject = (GodotObject)gds.New();
        var dbOject2 = (GodotObject)gds.New();
        this.dbOject.Call("loadDb", "res://data/demoDataDb");
        dbOject2.Call("loadDb", "res://data/demoDataDb");
        this.dbOject.Call("openDb", null); //打开数据库
        dbOject2.Call("openDb", null); //打开数据库
        //初始化所有数据
        Godot.Collections.Array allName = (Godot.Collections.Array)this.dbOject.Call("query_all_config_table", null);

        Godot.Collections.Array tableDataQuery;

        string tableName = "";
        foreach (var data in allName)
        {
            //GD.Print("表明是：" + data.AsGodotDictionary<string, string>()["name"]);
            List<JObject> alldata = new List<JObject>();
            tableName = data.AsGodotDictionary<string, string>()["name"];
            tableDataQuery = (Godot.Collections.Array)dbOject2.Call("query_table_data", tableName);
            foreach (var queryData in tableDataQuery)
            {
                var strings = queryData.ToString().Replace("<null>", "null");
                alldata.Add(((JObject)JsonConvert.DeserializeObject(strings)));

            }
            this.configDict.Add(tableName, alldata);
            //tableDataQuery.Clear();


        }

        this.dbOject.Call("closeDb", null);
        dbOject2.Call("closeDb", null);

    }
    ///<remarks>
    ///查找所有配置
    ///</remarks>
    public List<T> findAllConfig<T>(string tableName)
    {

        List<T> result = new List<T>();
        List<JObject> arrayData = new List<JObject>();
        //查看是否有缓存
        if (this.configDict.ContainsKey(tableName))
        {
            //有缓存直接读
            arrayData = this.configDict[tableName];
            GD.Print("这里是缓存");
        }

        foreach (var data in arrayData)
        {

            result.Add(data.ToObject<T>());

        }

        return result;
    }

    ///<remarks>
    ///根据条件查询,数据库
    ///</remarks>
    public List<T> findConfigByConditionInDb<T>(string tableName, string condition)
    {

        List<T> result = new List<T>();
        this.dbOject.Call("openDb", null); //打开数据库
        Godot.Collections.Array arrayData = (Godot.Collections.Array)this.dbOject.Call("query_table_condition", tableName, condition);
        this.dbOject.Call("closeDb", null);
        foreach (var data in arrayData)
        {

            var strings = data.ToString().Replace("<null>", "null");
            GD.Print(strings);
            result.Add(JsonConvert.DeserializeObject<T>(strings));
        }
        //GD.Print(arrayData);
        return result;
    }


}